﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebProjectOrder.WebServices;

namespace WebProjectOrder.BusinessLayer
{
    public class CurrencyManager
    {

        #region Atributes
        private Dictionary<int, Currency> _currencies;
        private Currency _localCurrency;
        private Currency _systemCurrency;
        private bool _isError;
        private bool _definiteError;
        private String _errorMessage;
        #endregion

        #region Singleton
        private CurrencyManager() 
        {
            _currencies = new Dictionary<int, Currency>();
            _isError = _definiteError = false;
            List<Currency> currencies = WebServices.WebServices.getInstance().getCurrencies();
            if (currencies != null)
            {
                foreach (Currency currency in currencies)
                {
                    if (currency.Type.Equals(Constants.CurrencyType.added))
                        _currencies.Add(currency.Id, currency);
                    else if (currency.Type.Equals(Constants.CurrencyType.local))
                        if (_localCurrency == null)
                            _localCurrency = currency;
                        else
                        {
                            _isError = true;
                            _errorMessage = Constants.ERROR_MORE_LOCAL_CURRENCIES;
                        }
                    else if (currency.Type.Equals(Constants.CurrencyType.system))
                        if (_systemCurrency == null)
                            _systemCurrency = currency;
                        else
                        {
                            _isError = true;
                            _errorMessage = Constants.ERROR_MORE_SYSTEM_CURRENCIES;
                        }
                }
                if (_localCurrency == null || _systemCurrency == null)
                {
                    _isError = true;
                    _errorMessage = _localCurrency == null ? Constants.ERROR_MISSING_LOCAL_CURRENCY : Constants.ERROR_MISSING_SYSTEM_CURRENCY;
                }
            }
            else
            {
                _isError = true;
                _errorMessage = Constants.ERROR_DB_CONNECTION;
            }
            _definiteError = _isError;
        }


        /// <summary>
        /// Unica Instancia de esta clase
        /// </summary>
        private static CurrencyManager _instance;


        /// <summary>
        /// Método para implementar el patrón de singleton
        /// </summary>
        /// <returns>Instancia única de la clase</returns>
        public static CurrencyManager getInstance() 
        {
            if (_instance == null)
                _instance = new CurrencyManager();
            return _instance;
        }

        #endregion


        #region Métodos
        /// <summary>
        /// Método que busca todas las monedas existentes, sin contar la del sistema ni local
        /// </summary>
        /// <returns>Lista de Monedas sin local ni sistema</returns>
        public List<Currency> GetCurrencies()
        {
            List<Currency> currencyList = new List<Currency>();
            foreach (Currency currency in _currencies.Values)
                currencyList.Add(currency);
            return currencyList;
        }
        #endregion
    }
}